import {
  getDictTreeData
} from "@/api/common.js";
import {
  brandStockList, //获取厂商
} from "@/api/standardManagement/equipmentBrand.js";

//整理数据字典data
const fmDictData = (data) => {
  var obj = new Object();
  if (data) {
    data.map((i) => {
      if (i.sysDataDictDetailList) {
        obj[i.code] = i.sysDataDictDetailList;
        Object.assign(obj, fmDictData(i.sysDataDictDetailList));
      }
    });
  }
  return obj;
};

const common = {
  state: {
    systemCode: "",
    vendorList: [],
    brandList: [],
    menuPictures: [],
    dictData: {}, //数据字典数据
    points: []
  },
  mutations: {
    SET_DICTDATA: (state, data) => {
      state.dictData = fmDictData(data);
    },
    SET_VENDORLIST: (state, data) => {
      state.vendorList = data;
    },
    SET_BRANDLIST: (state, data) => {
      state.brandList = data;
    },
    SET_NOPRATOL: (state, data) => {
      state.noPartol = data;
    },
  },
  actions: {
    GetDictData({
      commit
    }) {
      return new Promise((resolve, reject) => {
        Promise.all([getDictTreeData()]).then(res => {
          let dictData = res[0].data || [];
          commit("SET_DICTDATA", dictData);
          resolve(true);
        })
          .catch((error) => {
            reject(error);
          });
      });
    },
    GetBrandData({
      commit
    }) {
      return new Promise((resolve, reject) => {
        brandStockList()
          .then((res) => {
            let brandList = res.data.records || [];
            let brandListArr = [];
            let vendorListArr = [];
            brandList.map((item) => {
              item.brandStockList.map(j => {
                let {
                  factoryName,
                  factoryId,
                  name,
                  id
                } = j;
                let brandObj = {
                  factoryId,
                  factoryName,
                  name,
                  label: name,
                  value: id,
                }
                brandListArr.push(brandObj)
              })
              let obj = {
                label: item.factoryName,
                value: item.factoryId,
              }
              vendorListArr.push(obj)
            });
            commit("SET_BRANDLIST", brandListArr);
            commit("SET_VENDORLIST", vendorListArr);
            resolve(true);

          })
      });
    },
    SetNoPratolData({
      commit
    }, param) {
      return new Promise((resolve, reject) => {
        commit("SET_NOPRATOL", param);
        resolve(true);
      }).catch((error) => {
        reject(error);
      })

    },
    // GetDictData({
    //   commit
    // }) {
    //   return new Promise((resolve, reject) => {
    //     Promise.all([getDictTreeData(), brandStockList()]).then(res => {
    //         let dictData = res[0].data || [];
    //         commit("SET_DICTDATA", dictData);
    //         let brandList = res[1].data || [];
    //         let brandListArr = [];
    //         let vendorListArr = [];
    //         brandList.map((item) => {
    //           item.brandStockList.map(j => {
    //             let {
    //               factoryName,
    //               factoryId,
    //               name,
    //               id
    //             } = j;
    //             let brandObj = {
    //               factoryId,
    //               factoryName,
    //               name,
    //               label: name,
    //               value: id,
    //             }
    //             brandListArr.push(brandObj)
    //           })
    //           let obj = {
    //             label: item.factoryName,
    //             value: item.factoryId,
    //           }
    //           vendorListArr.push(obj)
    //         });
    //         commit("SET_BRANDLIST", brandListArr);
    //         commit("SET_VENDORLIST", vendorListArr);
    //         resolve(true);
    //       })
    //       .catch((error) => {
    //         reject(error);
    //       });
    //   });
    // },
  },
};
export default common;